<?php
namespace Home\Controller;
use Think\Controller;

  
 
class HomePageController extends Controller {
	
	

	//大厅首页
    public function index(){
			
	$keyWord = I('get.room');	 //关键词搜索房间
	$GroupRoom = I('get.id');    //分区房间
    $R = D("tbvcbinformation");   // 房间表
	$items = 20;  //每页显示房间数	


	$UserId = getUserId();
	session('homeId',$UserId) ;

	
	if($GroupRoom){
		if($GroupRoom=="favorite"){     //客户收藏房间
			$F = D('tbvcbfavourite');
			$count = $F->table('tbvcbfavourite a,tbvcbinformation b')
			->where("a.nvcbid=b.nvcbid and a.nuserid=$UserId")
			->count();		
		}else if($GroupRoom=="viewed"){   //客户浏览房间
			$V = D('tbvcbrecent');
			$count = $V->table('tbvcbrecent a,tbvcbinformation b')
			->where("a.nvcbid=b.nvcbid and a.nuserid=$UserId")
			->count();			
		
		}else{                           //分区房间
			$count = $R->where("ngroup=$GroupRoom")
			->count();
			}
	}else if($keyWord!=''){
		if(is_numeric($keyWord)){
			$count = $R->where("nvcbid=$keyWord")
			->count();
			}else{
				$count = $R->where("a.cname like '%$keyWord%'")
				->count();

			}
	}else{
			$count = $R->field('cname,image,nvcbid,nseats,nusercount')->count();
	}	
	if(!$count){
		$count=0;
	}
	 
	$Page = new \Think\Page($count,$items);
	
	if($GroupRoom){		
		if($GroupRoom=="favorite"){     //客户收藏房间
			$F = D('tbvcbfavourite');
			$room = $F->table('tbvcbfavourite a,tbvcbinformation b')
			->field('a.nid,b.cname,b.nusercount,b.image,b.nvcbid,b.nseats')
			->where("a.nvcbid=b.nvcbid and a.nuserid=$UserId")
			->order('b.nusercount desc')
			->select();
			
		}else if($GroupRoom=="viewed"){   //客户浏览房间
			$V = D('tbvcbrecent');
			$room = $V->table('tbvcbrecent a,tbvcbinformation b')
			->field('a.nid,b.cname,b.image,b.nvcbid,b.nseats,b.nusercount')
			->where("a.nvcbid=b.nvcbid and a.nuserid=$UserId")
			->order('b.nusercount desc')
			->select();		

		}else{                           //分区房间
			
			$room = $R->field('cname,image,nvcbid,nseats,nusercount')
			->where("ngroup=$GroupRoom")
			->limit($Page->firstRow.','.$Page->listRows)
			->order('nusercount desc')			
			->select();
			}
	
	}else if($keyWord!=''){
		if(is_numeric($keyWord)){
			$room = $R->field('cname,image,nvcbid,nseats,nusercount')
			->where("nvcbid=$keyWord")
			->limit($Page->firstRow.','.$Page->listRows)
			->order('nusercount desc')						
			->select();
			}else{
				$room = $R->field('cname,image,nvcbid,nseats,nusercount')
				->where("cname like '%$keyWord%'")
				->limit($Page->firstRow.','.$Page->listRows)
			    ->order('nusercount desc')							
				->select();
			}
	}else{
			$room = $R->field('cname,image,nvcbid,nseats,nusercount')
			->limit($Page->firstRow.','.$Page->listRows)
			->order('nusercount desc')			
			->select();
	}
	if(!$room){
		$room = '';
	}

		$show = $Page->show(); 

		$this->assign('rooms',$room);   //房间列表赋值	
		$this->assign('list',$show);   //分布标签赋值
		
		//获取游戏目录
		$Game = M('tbgameinformation')->field('cname,ngameid,nonline')->where('nindex1>0')->select();
		$this->assign('games',$Game);  //游戏列表赋值
		
		//获取视频分区目录
		$G = M('Tbgroupinfomation');
		$TopMap['nparentid']  = 0;	
		$TopG = $G->field('ngroupid,cname')->where($TopMap)->select();	
		foreach($TopG  as $val){
		
			$ChildMap['nparentid']  = $val['ngroupid'];			
			$childName = array();

			$child = $G->field('ngroupid,cname')->where($ChildMap)->select();  //视频分区目录 							
			if($child){
			foreach($child as $sub){
				$subGid = $sub['ngroupid'];
				$online = $G->table('tbgroupinfomation a,tbvcbinformation b')
							->field('a.ngroupid,b.nvcbid,b.nusercount')
							->where("a.ngroupid=b.ngroup and a.ngroupid=$subGid")
							->sum('nusercount');    //视频分区下的所有房间
				if(!$online){
				$online=0;
				}
				$childName[] = array($sub['cname'],$sub['ngroupid'],$online);	
				
			}			
			$Menu[] =array(
				  'Top' =>$val['cname'],
				  'Child'=> $childName ,
				  );
			}
		}
	
	
		$this->assign('menu',$Menu);    //目录赋值 
		
		
        //获取轮播图片
		$B = M('tbimagead');
		$Imap['nplace']=1;
		$Imap['nstatus']=1;
		$images = $B->field('cimage,clink,nroomid')->where($Imap)->select();		
		$this->assign('image',$images);    //轮播图赋值 

		//排行
		$thisMonth = date('Y-m-01');  //本月日期
		
		//财富排行
		$W = M('tbuserconsumemonth');
		$wealth = $W->table('tbuserconsumemonth a,tbuserinformation b')
				->field('a.nmoney,a.nuserid,b.calias')
				->where("a.nuserid=b.nuserid and a.ddate='$thisMonth'")
				->order('nmoney desc')
				->limit(20)
				->select();		
		$this->assign('weath',$wealth); 

		//人气排行			
		$S = M('tbuserreceivemonth');
		$service = $S->table('tbuserreceivemonth a,tbuserinformation b')
				->field('a.nmoney,a.nuserid,b.calias')
				->where("a.nuserid=b.nuserid and a.ddate='$thisMonth'")
				->order('nmoney desc')
				->limit(20)
				->select();	
		$this->assign('service',$service); 
		 //房间排行 
		$F = M('tbvcbscoremonth');
		$Fdata = $F->table('tbvcbscoremonth a,tbvcbinformation b')
				->field('a.nvcbid,a.fscore,b.cname')
				->where("a.nvcbid=b.nvcbid and a.ddate='$thisMonth'")
				->order('fscore desc')
				->limit(20)
				->select();			
		$this->assign('fangjian',$Fdata);   //房间排行赋值
		
		//在线人数
		$O = M('tbvcbinformation');
		//$now = time();
		$online = $O->field('SUM(nusercount) as online')->select();
		$online = $online[0]['online'];
		
		$this->assign('online',$online);   //房间排行赋值
		
		//奖池
		$shuahua_rewards = M('tbglobalvalue')->field('nvalue')->where("cname='gift_prize_pool'")->select();
		$game_rewards = M('tbglobalvalue')->field('nvalue')->where("cname='ppc_prizepool'")->select();
		$shuahua_rewards = $shuahua_rewards[0]['nvalue'];
		$game_rewards = $game_rewards[0]['nvalue'];
		$shuahuaLenth = strlen($shuahua_rewards);
		$gameLenth = strlen($game_rewards);
		
		if($shuahuaLenth>4 && $shuahuaLenth<9){		
			$shuahua_rewards = substr($shuahua_rewards,0,($length-4))."万";
		}else if($shuahuaLenth>8){
			$shuahua_rewards = substr($shuahua_rewards,0,($length-8))."亿";		
		}
		if($gameLenth>4 && $gameLenth<9){		
			$game_rewards = substr($game_rewards,0,($length-4))."万";
		}else if($gameLenth>8){
			$game_rewards = substr($game_rewards,0,($length-8))."亿";		
		}		
		
		$this->assign('shuahua_rewards',$shuahua_rewards);   //刷花
		$this->assign('game_rewards',$game_rewards);   //游戏
		//$this->display('nindex');   	
		$this->display();
    }
	
	
	
	//数据操作
	public function oper(){
		
		if($_POST['func']=='del')
			if($_POST['type']=="viewed"){
				$M = M('tbvcbrecent');
			}else{
				$M=M('tbvcbfavourite');
			}
			
			$nid = $_POST['nid'];
			
			if($M->where("nid=$nid")->delete()){
			 echo 1; //删除成功
			};
		
	}
	
	
	//排行榜
	public function rank(){


	$thisWeek= date('Y-m-d',strtotime("-1 Sunday"));   //本周日期
	$lastWeek = date('Y-m-d',strtotime("-2 Sunday")); //上周日期
	$thisMonth = date('Y-m-01');  //本月日期
	$lastMonth = date('Y-m-d',mktime(0,0,0,date('m')-1,1,date('Y'))); //上月日期

	$category = $_GET['cate'];
	if($category=='' || $category=='renqi'){   //财富排行和人气排行.财富排行默认为空
		if($category==''){
			$weekTable = 'tbuserconsumeweek';
			$monthTable = 'tbuserconsumemonth';
			$week_table = M("$weekTable");				
			$month_table = M("$monthTable");
		}else if($category=='renqi'){
			$weekTable = 'tbuserreceiveweek';
			$monthTable = 'tbuserreceivemonth';		
			$week_table = M("$weekTable");	
			$month_table = M("$monthTable");		
		}				
		$Tweek_data = $week_table->table("$weekTable a,tbuserinformation b")
				->field('a.nmoney,a.nuserid,b.calias')
				->where("a.nuserid=b.nuserid and a.ddate='$thisWeek'")
				->order('nmoney desc')
				->limit(30)
				->select();	
		$Lweek_data = $week_table->table("$weekTable a,tbuserinformation b")
				->field('a.nmoney,a.nuserid,b.calias')
				->where("a.nuserid=b.nuserid and a.ddate='$lastWeek'")
				->order('nmoney desc')
				->limit(30)
				->select();	
		$Tmonth_data = $month_table->table("$monthTable a,tbuserinformation b")
				->field('a.nmoney,a.nuserid,b.calias')
				->where("a.nuserid=b.nuserid and a.ddate='$thisMonth'")
				->order('nmoney desc')
				->limit(30)
				->select();	
		$Lmonth_data = $month_table->table("$monthTable a,tbuserinformation b")
				->field('a.nmoney,a.nuserid,b.calias')
				->where("a.nuserid=b.nuserid and a.ddate='$lastMonth'")
				->order('nmoney desc')
				->limit(30)
				->select();		
	}else if($category=='fangjian' || $category=='shuahua'){   //房间排列和房间刷花排列
			$weekTable = 'tbvcbscoreweek';
			$monthTable = 'tbvcbscoremonth';
			$week_table = M("$weekTable");				
			$month_table = M("$monthTable");

		if($category=='fangjian' ){
			$filed = 'fscore';
		}else if($category=='shuahua'){
			$filed = 'nmoney';
		}
		$Tweek_data = $week_table->table("$weekTable a,tbvcbinformation b")
				->field("a.$filed,a.nvcbid,b.cname")
				->where("a.nvcbid=b.nvcbid and a.ddate='$thisWeek'")
				->order("$filed desc")
				->limit(30)
				->select();	
		$Lweek_data = $week_table->table("$weekTable a,tbvcbinformation b")
				->field("a.$filed,a.nvcbid,b.cname")
				->where("a.nvcbid=b.nvcbid and a.ddate='$lastWeek'")
				->order("$filed desc")
				->limit(30)
				->select();	
		$Tmonth_data = $month_table->table("$monthTable a,tbvcbinformation b")
				->field("a.$filed,a.nvcbid,b.cname")
				->where("a.nvcbid=b.nvcbid and a.ddate='$thisMonth'")
				->order("$filed desc")
				->limit(30)
				->select();	
		$Lmonth_data = $month_table->table("$monthTable a,tbvcbinformation b")
				->field("a.$filed,a.nvcbid,b.cname")
				->where("a.nvcbid=b.nvcbid and a.ddate='$lastMonth'")
				->order("$filed desc")
				->limit(30)
				->select();		
	}
	
		$this->assign('thisweek',$Tweek_data);   //本周排名 
		$this->assign('lastweek',$Lweek_data);   //上周排名 
		$this->assign('thismonth',$Tmonth_data);   //本月排名 
		$this->assign('lastmonth',$Lmonth_data);   //上月排名 

		$this->display();
	
	}


}